<!--
 * Description: SupplyCloud主页面
 * Company: 联宝（合肥）电子科技有限公司
 * Author: Dongxu.Ni
 * Date: 2020/04/14 02:52
-->

<template>
  <BaseWrap>
    <!-- 查询条件 -->
    <BaseFunc
      :is-requesting="isRequesting"
      :query-form="queryForm"
      :visible.sync="advanceOptionDrawer"
      @query="query"
    >
      <!-- 按钮 -->
      <template slot="btn">
        <el-button
          v-if="$hasPer('add')"
          class="btn-custom"
          icon="el-icon-circle-plus-outline"
          plain
          size="mini"
          type="primary"
          @click="addDialogVisible = true"
        >
          {{ $t('button.add') }}
        </el-button>
      </template>

      <!-- 同步按钮 -->
      <template slot="btn-middle">
        <el-input
          v-model.trim="syncInput"
          class="el-inputbtn"
          clearable
          maxlength="6"
          oninput="value=value.replace(/^\.+|[^\d.]/g,'')"
          placeholder="请输入年月格式:YYYYMM"
        >
          >
        </el-input>
        <el-button
          class="btn-custom"
          :disabled="this.syncInput == '' ? true : false"
          icon="el-icon-search"
          :loading="IsSyncInput"
          plain
          size="mini"
          type="primary"
          @click="syncBtn"
        >
          {{ '查询' }}
        </el-button>
      </template>

      <!--抽屉内部查询表单-->
      <template>
        <el-form class="g-advance-drawer-form" label-width="140px" :model="queryForm" size="medium">
          <el-row>
            <!--pn-->
            <el-col :span="6">
              <el-form-item label="PN">
                <el-input v-model="queryForm.pn" clearable />
              </el-form-item>
            </el-col>

            <!--vendor-->
            <el-col :span="6">
              <el-form-item label="vendor">
                <el-input v-model="queryForm.vendor" clearable />
              </el-form-item>
            </el-col>

            <!--sourceCode-->
            <el-col :span="6">
              <el-form-item label="SourceCode">
                <el-input v-model="queryForm.sourceCode" clearable />
              </el-form-item>
            </el-col>

            <!--actualGr-->
            <el-col :span="6">
              <el-form-item label="ActualGr">
                <el-input v-model="queryForm.actualGr" clearable />
              </el-form-item>
            </el-col>

            <!--outlookGr-->
            <el-col :span="6">
              <el-form-item label="OutlookGr">
                <el-input v-model="queryForm.outlookGr" clearable />
              </el-form-item>
            </el-col>

            <!--fiscalYear-->
            <el-col :span="6">
              <el-form-item label="FiscalYear">
                <el-input v-model="queryForm.fiscalYear" clearable />
              </el-form-item>
            </el-col>

            <!--month-->
            <el-col :span="6">
              <el-form-item label="Month">
                <el-input v-model="queryForm.month" clearable />
              </el-form-item>
            </el-col>

            <!--mrpOutlookGr-->
            <el-col :span="6">
              <el-form-item label="MrpOutlookGr">
                <el-input v-model="queryForm.mrpOutlookGr" clearable />
              </el-form-item>
            </el-col>

            <!--mrpOutlookGrNumber-->
            <el-col :span="6">
              <el-form-item label="MrpOutlookGrNumber">
                <el-input v-model="queryForm.mrpOutlookGrNumber" clearable />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </template>
    </BaseFunc>

    <!--报价commodity数据表格-->
    <BaseTable
      :is-requesting="isRequesting"
      :page-number.sync="pageNumber"
      :page-size.sync="pageSize"
      :table-data="tableData"
      :total="total"
      @query="query"
    >
      <template slot="header">
        <!--序号列-->
        <el-table-column align="center" fixed :label="$t('table.NO')" min-width="50px">
          <template slot-scope="scope">
            <span>{{ scope.$index + 1 }}</span>
          </template>
        </el-table-column>
      </template>

      <template>
        <!--PN-->
        <el-table-column align="center" label="PN" min-width="200px" prop="pn" sortable />
        <!-- vendor -->
        <el-table-column align="center" label="vendor" min-width="150px" prop="vendor" sortable />
        <!--sourceCode-->
        <el-table-column
          align="center"
          label="Source Code"
          min-width="200px"
          prop="sourceCode"
          :sort-method="$sortByLower"
          sortable
        />
        <!--actualGr-->
        <el-table-column
          align="center"
          label="ActualGr"
          min-width="200px"
          prop="actualGr"
          sortable
        />
        <!--outlookGr-->
        <el-table-column
          align="center"
          label="OutlookGr"
          min-width="200px"
          prop="outlookGr"
          sortable
        />
        <!--fiscalYear-->
        <el-table-column
          align="center"
          label="FiscalYear"
          min-width="200px"
          prop="fiscalYear"
          sortable
        />
        <!--month-->
        <el-table-column align="center" label="Month" min-width="200px" prop="month" sortable />
        <!--mrpOutlookGr-->
        <el-table-column
          align="center"
          label="MrpOutlookGr"
          min-width="200px"
          prop="mrpOutlookGr"
          sortable
        />
        <!--mrpOutlookGrNumber-->
        <el-table-column
          align="center"
          label="MrpOutlookGrNumber"
          min-width="200px"
          prop="mrpOutlookGrNumber"
          sortable
        />
        <!--creator 创建人-->
        <!-- <el-table-column
          align="center"
          :label="$t('SupplyCloud.creator')"
          min-width="100px"
          prop="creator"
          sortable
        >
          <template slot-scope="scope">
            <span>
              {{ $store.getters['user/getUserNameById'](scope.row.creator) || $t('result.none') }}
            </span>
          </template>
        </el-table-column> -->
        <!--createDate 创建时间-->
        <el-table-column
          align="center"
          :label="$t('SupplyCloud.createDate')"
          min-width="200px"
          prop="createTime"
          sortable
        />
      </template>

      <!-- 右浮动 -->
      <template slot="end">
        <!--操作列-->
        <el-table-column
          align="center"
          fixed="right"
          :label="$t('table.operate')"
          min-width="150px"
        >
          <template slot-scope="scope">
            <el-button
              v-if="$hasPer('edit')"
              size="mini"
              type="text"
              @click="rowEdit(scope.$index, scope.row)"
            >
              <i class="el-icon-edit el-icon--right"></i>
              {{ $t('button.edit') }}
            </el-button>
            <el-button
              v-if="$hasPer('delete')"
              size="mini"
              type="text"
              @click="rowDelete(scope.$index, scope.row)"
            >
              <i class="el-icon-delete el-icon--right"></i>
              {{ $t('button.delete') }}
            </el-button>
          </template>
        </el-table-column>
      </template>
    </BaseTable>

    <!--新增对话框-->
    <BaseDialog
      v-if="addDialogVisible"
      :title="$t('dialog.add')"
      :visible.sync="addDialogVisible"
      width="55%"
    >
      <MonthGRAdd @cancel="addDialogVisible = false" @save="afterAddSave" />
    </BaseDialog>

    <!--编辑对话框-->
    <BaseDialog
      v-if="editDialogVisible"
      :title="$t('dialog.edit')"
      :visible.sync="editDialogVisible"
      width="55%"
    >
      <MonthGREdit
        :input-data="currentEditRow"
        @cancel="editDialogVisible = false"
        @save="afterEditSave"
      />
    </BaseDialog>
  </BaseWrap>
</template>

<script>
import {
  monthOutlookActGRPageList,
  monthOutlookActGRDelete
} from '@/api/spendingSaving/spendingSaving'
import { sapQuerySapMrpGrInfo } from '@/api/quote/sap'
import MonthGRAdd from './components/MonthGRAdd'
import MonthGREdit from './components/MonthGREdit'

export default {
  name: 'MonthOutlookActGR',
  components: { MonthGRAdd, MonthGREdit },
  props: {},
  data() {
    return {
      // 正在查询
      isRequesting: false,
      // 高级查询选项抽屉弹出标识
      advanceOptionDrawer: false,
      // 页码
      pageNumber: 1,
      // 页展示行数
      pageSize: 10,
      // 总行数
      total: 0,
      // 表格数据
      tableData: [],
      // 查询表单对象
      queryForm: {
        actualGr: '',
        createTime: '',
        fiscalYear: '',
        month: '',
        mrpOutlookGr: '',
        mrpOutlookGrNumber: '',
        outlookGr: '',
        pn: '',
        sourceCode: '',
        vendor: ''
      },
      // 新增对话框可见标识
      addDialogVisible: false,
      // 编辑对话框可见标识
      editDialogVisible: false,
      // 当前编辑行的数据
      currentEditRow: {},
      syncInput: '',
      IsSyncInput: false
    }
  },
  computed: {},
  watch: {},
  created() {
    this.pageDataInit()
  },
  mounted() {},
  methods: {
    /* 页面数据初始化*/
    pageDataInit() {
      this.query()
    },
    /* 查询表格数据*/
    async query() {
      try {
        this.isRequesting = true
        this.advanceOptionDrawer = false
        const res = await monthOutlookActGRPageList({
          pageSize: this.pageSize,
          pageNumber: this.pageNumber,
          pageCondition: this.queryForm
        })
        this.tableData = res.data.list
        this.total = res.data.total
      } catch (error) {
        console.log(error)
      } finally {
        this.isRequesting = false
      }
    },
    /* 行记录编辑*/
    rowEdit(index, row) {
      this.currentEditRow = row
      this.editDialogVisible = true
    },
    /* 行记录删除 */
    async rowDelete(index, row) {
      try {
        await this.$confirmBox(this.$t('button.delete'), 'error')
        this.isRequesting = true
        await monthOutlookActGRDelete({ pnId: row.pnId })
        this.query()
        // this.$store.dispatch('commodity/initCommodityCollects')
      } catch (error) {
        console.log(error)
      } finally {
        this.isRequesting = false
      }
    },
    // 新增保存之后
    afterAddSave() {
      this.addDialogVisible = false
      this.query()
      // this.$store.dispatch('commodity/initCommodityCollects')
    },
    // 修改保存之后
    afterEditSave() {
      this.editDialogVisible = false
      this.query()
      // this.$store.dispatch('commodity/initCommodityCollects')
    },
    syncBtn() {
      try {
        if (this.syncInput.length !== 6) {
          this.$message.error('输入 YYYYMM 格式的年月')
          return false
        }
        this.IsSyncInput = true
        sapQuerySapMrpGrInfo({ date: this.syncInput })
      } catch (error) {
        console.log(error)
      } finally {
        this.isRequesting = false
        this.IsSyncInput = false
      }
    }
  }
}
</script>

<style scoped>
.el-inputbtn {
  margin-right: 2px;
}
.el-inputbtn /deep/ .el-input__inner {
  height: 30px;
  line-height: 30px;
}
</style>
